﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows;

namespace AssignmentsManage.Converters
{
    public class CourseCodeToCourseObjectConverter : DependencyObject, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && Holder != null)
            {
                if (value.GetType() == typeof(string))
                {
                    string code = value.ToString();
                    return Holder.getCourseByCode(code);
                }
                else if (value.GetType() == typeof(DataClasses.Course))
                {
                    DataClasses.Course course = (DataClasses.Course)value;
                    return course.CourseCode;
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && Holder != null)
            {
                if (value.GetType() == typeof(string))
                {
                    string code = value.ToString();
                    return Holder.getCourseByCode(code);
                }
                else if (value.GetType() == typeof(DataClasses.Course))
                {
                    DataClasses.Course course = (DataClasses.Course)value;
                    return course.CourseCode;
                }
            }

            return null;
        }

        // Dependency Property
        public static readonly DependencyProperty HolderProperty =
             DependencyProperty.Register("Holder", typeof(DataClasses.DataHolder),
             typeof(CourseCodeToCourseObjectConverter), new FrameworkPropertyMetadata(null));

        // .NET Property wrapper
        public DataClasses.DataHolder Holder
        {
            get { return (DataClasses.DataHolder)GetValue(HolderProperty); }
            set { SetValue(HolderProperty, value); }
        }

    }
}
